/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.todoterreno.beans;

import com.todoterreno.dao.GestionImagenesInter;
import com.todoterreno.dao.impl.GestionImagenesInterDao;
import com.todoterreno.model.CasaTerreno;
import com.todoterreno.utils.AddressConverter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

/**
 *
 * @author davex
 */
public class CompraCercaBean implements Serializable{

    private String root = "/home/davidpanel/fotos/";
    private String rootplots = "/home/davidpanel/plots/";
    private List<CasaTerreno> images;
    private MapModel simpleMapModel;
    private GestionImagenesInter obtImagen = new GestionImagenesInterDao();
    private StreamedContent streamImagen;
    private StreamedContent streamImagenPlot;
    private String depto;
    private String valorDireccion;
    private String nombrePlot;
    
    public CompraCercaBean() {
        if(depto==null){
            depto="SAN SALVADOR";
        }
        updateData();
    }
    
    public void updateData(){
        images=obtImagen.consultarCasas(4, depto);
//        for(CasaTerreno ct: images){
//            System.out.println("Dat: "+ct.getNombreplano());
//        }
        nombrePlot="";
//        simpleMapModel=new DefaultMapModel();
    }

    public void mostrarMapa() throws IOException{
        RequestContext reqCon=RequestContext.getCurrentInstance();
        String direc = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("direc");
        System.out.println("Direcccion: "+direc);
        String coordenadas=AddressConverter.buscaLocalizacion(direc);
        String[] latitude=new String[2];
        if(coordenadas.equals("")){
            System.out.println("No se encontraron las coordenadas");
            latitude="13.706881, -89.21373179999999".split(",");
        }else{
            System.out.println("Se encontraron las coordenadas: "+coordenadas);
            latitude=coordenadas.split(",");
        }
        
        LatLng coord1=new LatLng(Double.parseDouble(latitude[0]), Double.parseDouble(latitude[1]));
        simpleMapModel=new DefaultMapModel();
        simpleMapModel.addOverlay(new Marker(coord1));
//        simpleMapModel.addOverlay(new Marker(coord1, "Mapaaaaaa"));
        
        reqCon.update(":form:mapa");
        reqCon.execute("dialogShow.show()");
    }
    
    public void mostrarPlot(){
        System.out.println("Planooo: "+nombrePlot);
        
        RequestContext reqCon=RequestContext.getCurrentInstance();
        
        reqCon.update(":form:plotImagen");
        reqCon.execute("dialogPlotShow.show()");
        
    }
    
    public String getDepto() {
        return depto;
    }

    public void setDepto(String depto) {
        this.depto = depto;
    }

    public List<CasaTerreno> getImages() {
        return images;
    }

    public void setImages(List<CasaTerreno> images) {
        this.images = images;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }

    public MapModel getSimpleMapModel() {
        return simpleMapModel;
    }

    public void setSimpleMapModel(MapModel simpleMapModel) {
        this.simpleMapModel = simpleMapModel;
    }

    public StreamedContent getStreamImagen() {
        try {

            FacesContext context = FacesContext.getCurrentInstance();
//            if (context.getRenderResponse()) {
            if(context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE){                    
                return new DefaultStreamedContent();
            } else {
                String name = context.getExternalContext().getRequestParameterMap().get("nameImage");
//                valorDireccion=context.getExternalContext().getRequestParameterMap().get("direc");
                File imgFile = new File(root + name);
                if(imgFile.isDirectory()){
                    StreamedContent ds = new DefaultStreamedContent();
                    return ds;
                }else{
                    StreamedContent ds = new DefaultStreamedContent(new FileInputStream(imgFile), "image/jpg");
                    return ds;
                }
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setStreamImagen(StreamedContent streamImagen) {
        this.streamImagen = streamImagen;
    }

    public String getValorDireccion() {
        return valorDireccion;
    }

    public void setValorDireccion(String valorDireccion) {
        this.valorDireccion = valorDireccion;
    }

    public String getRootplots() {
        return rootplots;
    }

    public void setRootplots(String rootplots) {
        this.rootplots = rootplots;
    }

    public StreamedContent getStreamImagenPlot() {
        try {

            FacesContext context = FacesContext.getCurrentInstance();
//            if (context.getRenderResponse()) {
            if(context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE){  
                return new DefaultStreamedContent();
            } else {
                String name = context.getExternalContext().getRequestParameterMap().get("plotImage");
//               System.out.println("Nombreplot: "+nombrePlot);
                File imgFile = new File(rootplots + name);
                if(imgFile.isDirectory()){
                    StreamedContent ds = new DefaultStreamedContent();
                    return ds;
                }else{
                    StreamedContent ds = new DefaultStreamedContent(new FileInputStream(imgFile), "image/jpg");
                    return ds;
                }
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setStreamImagenPlot(StreamedContent streamImagenPlot) {
        this.streamImagenPlot = streamImagenPlot;
    }

    public String getNombrePlot() {
        return nombrePlot;
    }

    public void setNombrePlot(String nombrePlot) {
        this.nombrePlot = nombrePlot;
    }
    
    
    
    
}
